package com.ruoyi.basic.service.impl;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.basic.domain.BasicUser;
import com.ruoyi.basic.mapper.BasicUserMapper;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.basic.mapper.BasicEducateMapper;
import com.ruoyi.basic.domain.BasicEducate;
import com.ruoyi.basic.service.IBasicEducateService;
import com.ruoyi.common.core.text.Convert;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 教育经历Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-08-22
 */
@Service
public class BasicEducateServiceImpl extends ServiceImpl<BasicEducateMapper, BasicEducate> implements IBasicEducateService
{
    @Autowired
    private BasicUserMapper userMapper;

    /**
     * 查询教育经历
     * 
     * @param educateId 教育经历主键
     * @return 教育经历
     */
    @Override
    public BasicEducate selectBasicEducateByEducateId(Long educateId)
    {
        return baseMapper.selectBasicEducateByEducateId(educateId);
    }

    /**
     * 查询教育经历列表
     * 
     * @param basicEducate 教育经历
     * @return 教育经历
     */
    @Override
    public List<BasicEducate> selectBasicEducateList(BasicEducate basicEducate)
    {
        return baseMapper.selectBasicEducateList(basicEducate);
    }

    /**
     * 新增教育经历
     * 
     * @param basicEducate 教育经历
     * @return 结果
     */
    @Override
    public int insertBasicEducate(BasicEducate basicEducate)
    {
        basicEducate.setCreateTime(DateUtils.getNowDate());
        return baseMapper.insert(basicEducate);
    }

    /**
     * 修改教育经历
     * 
     * @param basicEducate 教育经历
     * @return 结果
     */
    @Override
    public int updateBasicEducate(BasicEducate basicEducate)
    {
        basicEducate.setUpdateTime(DateUtils.getNowDate());
        return baseMapper.updateById(basicEducate);
    }

    /**
     * 批量删除教育经历
     * 
     * @param educateIds 需要删除的教育经历主键
     * @return 结果
     */
    @Override
    public int deleteBasicEducateByEducateIds(String educateIds)
    {
        return baseMapper.deleteBatchIds(Arrays.asList(Convert.toLongArray(educateIds)));
    }

    /**
     * 删除教育经历信息
     * 
     * @param educateId 教育经历主键
     * @return 结果
     */
    @Override
    public int deleteBasicEducateByEducateId(Long educateId)
    {
        return baseMapper.deleteById(educateId);
    }

    /**
     * 导入员工教育经历
     *
     * @param educateList 员工教育经历列表
     * @param operName 操作用户
     * @return 结果
     */
    @Override
    public String importEducate(List<BasicEducate> educateList, Boolean isUpdateSupport, String operName)
    {
        if (StringUtils.isNull(educateList) || educateList.size() == 0)
        {
            throw new ServiceException("导入员工教育经历数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (BasicEducate educate : educateList)
        {
            int row = 1;
            try
            {
                if(StringUtils.isEmpty(educate.getEducateName())){
                    continue;
                }
                //获取用户信息
                BasicUser user = userMapper.selectOne(new LambdaQueryWrapper<BasicUser>()
                        .eq(BasicUser::getIdCard, educate.getIdCard()).last("limit 1"));
                if(StringUtils.isNotNull(user)) {
                    educate.setUserId(user.getId());
                }else{
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、第 " + row + "行导入失败，员工 " + educate.getUserName() + " 不存在");
                    continue;
                }

                BasicEducate info = baseMapper.selectOne(new LambdaQueryWrapper<BasicEducate>()
                        .eq(BasicEducate::getEducateName, educate.getEducateName())
                        .eq(BasicEducate::getEducateDegree, educate.getEducateDegree())
                        .eq(BasicEducate::getEducateType, educate.getEducateType())
                        .eq(BasicEducate::getUserId, educate.getUserId()).last("limit 1"));
                if (StringUtils.isNull(info))
                {
                    educate.setCreateBy(operName);
                    this.insertBasicEducate(educate);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、员工 " + educate.getUserName() + "教育经历 " + educate.getEducateName() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    educate.setEducateId(info.getEducateId());
                    educate.setUpdateBy(operName);
                    this.updateBasicEducate(educate);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、员工 " + educate.getUserName() + "教育经历 " + educate.getEducateName() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、员工 " + educate.getUserName() + "教育经历 " + educate.getEducateName() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、员工 " + educate.getUserName() + " 教育经历导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
            finally {
                row++;
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
